package com.example.administrator.szb.activity.myyewu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.YWGLBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/2/25.
 */

public class MyYeWuAdapter  extends BaseAdapter {
    private Context context;
    private List list;
    LayoutInflater inflater;
    private boolean showCheckBox = false;
    private List<YWGLBean> select = new ArrayList();
    public MyYeWuAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }
    public void clearList(){
        select.clear();
    }
    public String getSelect(){
        String str = "";
        for (int i=0;i<select.size();i++){
            YWGLBean dataBean = (YWGLBean) select.get(i);
            str+=dataBean.getId()+",";
        }
        return str;
    }

    private void addBean(YWGLBean bean){
        if (!select.contains(bean)){
            select.add(bean);
        }
    }
    private void removeBean(YWGLBean bean){
        if (select.contains(bean)){
            select.remove(bean);
        }
    }
    private void operaBean(YWGLBean bean){
        if (select.contains(bean)){
            select.remove(bean);
        }else {
            select.add(bean);
        }
        notifyDataSetChanged();

    }

    public void setShowCheckBox(boolean showCheckBox) {
        this.showCheckBox = showCheckBox;
        clearList();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public  Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.view_ywgl_item2,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        final YWGLBean dataBean = (YWGLBean) list.get(position);
        holder.checkBox4.setClickable(false);
        holder.checkBox4.setEnabled(false);

        if (select.contains(dataBean)){
            holder.checkBox4.setImageResource(R.drawable.choose_check_new);
        }else {
            holder.checkBox4.setImageResource(R.drawable.unchoose_check_new);
        }
        if (dataBean.getNext().size() == 0){
            holder.imageView21.setVisibility(View.INVISIBLE);
        }else {
            holder.imageView21.setVisibility(View.VISIBLE);
        }
        holder.textView52.setText(dataBean.getTitle());
        if (showCheckBox){
            holder.checkBox4.setVisibility(View.VISIBLE);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    operaBean(dataBean);
                }
            });
        }else {
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener!=null){
                        listener.onItemClick(position);
                    }
                }
            });
            holder.checkBox4.setVisibility(View.GONE);
        }

        return convertView;
    }

    class ViewHolder{
        ImageView checkBox4;
        TextView textView52,textView57;
        ImageView imageView21;
        public ViewHolder(View view){
            imageView21 = view.findViewById(R.id.imageView21);
            textView52 = view.findViewById(R.id.textView52);
            textView57 = view.findViewById(R.id.textView57);
            checkBox4 = view.findViewById(R.id.checkBox4);
        }
    }
    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    interface OnItemClickListener{
        void onItemClick(int pos);
    }
}
